package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.entity.PushMessageTemplate;
import com.caocaog.market.mapper.PushMessageTemplateMapper;
import com.caocaog.market.service.PushMessageTemplateService;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.exception.CheckFailedException;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 推送消息模版管理
 *
 * @author chechen
 * @date 2022-05-21
 */
@Service
@Transactional
public class PushMessageTemplateServiceImpl extends ServiceImpl<PushMessageTemplateMapper, PushMessageTemplate> implements PushMessageTemplateService {

    private static final String LIST_CACHE_KEY = "'list'";

    @Override
    @Cacheable(value = CacheConstant.PUSH_MESSAGE_TEMPLATE_LIST, key = LIST_CACHE_KEY, sync = true)
    public List<PushMessageTemplate> getPushMessageTemplateList() {
        List<PushMessageTemplate> pushMessageTemplates = super.baseMapper.selectList(null);
        pushMessageTemplates.forEach(PushMessageTemplate::clearFields);
        return pushMessageTemplates;
    }

    @Override
    @Cacheable(value = CacheConstant.PUSH_MESSAGE_TEMPLATE_INFO, key = "#pushMessageTemplateId", sync = true)
    public PushMessageTemplate getPushMessageTemplateById(Integer pushMessageTemplateId) {
        PushMessageTemplate pushMessageTemplate = super.baseMapper.selectById(pushMessageTemplateId);
        if (pushMessageTemplate == null)
            throw new CheckFailedException("未找到此推送消息模版, pushMessageTemplateId-> " + pushMessageTemplateId);

        return pushMessageTemplate;
    }

    @Override
    @CacheEvict(value = CacheConstant.PUSH_MESSAGE_TEMPLATE_LIST, key = LIST_CACHE_KEY)
    public Integer addPushMessageTemplate(PushMessageTemplate pushMessageTemplate) {
        super.baseMapper.insert(pushMessageTemplate);
        if (pushMessageTemplate.getId() == null)
            throw new JeecgBootException("新增推送消息模版失败");

        return pushMessageTemplate.getId();
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PUSH_MESSAGE_TEMPLATE_LIST, key = LIST_CACHE_KEY),
            @CacheEvict(value = CacheConstant.PUSH_MESSAGE_TEMPLATE_INFO, key = "#pushMessageTemplate.id")
    })
    public int updatePushMessageTemplate(PushMessageTemplate pushMessageTemplate) {
        PushMessageTemplate oldPushMessageTemplate = this.getPushMessageTemplateById(pushMessageTemplate.getId());
        pushMessageTemplate.setLockVersion(oldPushMessageTemplate.getLockVersion() + 1);

        int updateRecordCount = super.baseMapper.updateById(pushMessageTemplate);
        if (updateRecordCount == 0)
            throw new JeecgBootException("更新推送消息模版失败, id-> " + pushMessageTemplate.getId());

        return updateRecordCount;
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PUSH_MESSAGE_TEMPLATE_LIST, key = LIST_CACHE_KEY),
            @CacheEvict(value = CacheConstant.PUSH_MESSAGE_TEMPLATE_INFO, key = "#pushMessageTemplateId")
    })
    public int deletePushMessageTemplate(Integer pushMessageTemplateId) {
        int deleteRecordCount = super.baseMapper.deleteById(pushMessageTemplateId);

        if (deleteRecordCount == 0)
            throw new JeecgBootException("删除推送消息模版失败, id-> " + pushMessageTemplateId);
        return deleteRecordCount;
    }

}
